﻿using System;
using System.Xml;

namespace CID.Domain.Infrastructure
{
    public static class ParameterValueSerializer
    {
        public static string Serialize(string value)
        {
            return value;
        }

        public static string Serialize(int value)
        {
            return XmlConvert.ToString(value);
        }

        public static string Serialize(decimal value)
        {
            return XmlConvert.ToString(value);
        }

        public static string Serialize(DateTime value)
        {
            return XmlConvert.ToString(value.ToUniversalTime(), XmlDateTimeSerializationMode.Utc);
        }

        public static string Serialize(bool value)
        {
            return XmlConvert.ToString(value);
        }

        public static string Serialize(Guid value)
        {
            return XmlConvert.ToString(value);
        }

        public static string Serialize(XmlDocument value)
        {
            return (value != null) ? value.OuterXml : null;
        }

        public static string DeserializeAsString(string value)
        {
            return value;
        }

        public static int DeserializeAsInteger(string value)
        {
            return XmlConvert.ToInt32(value);
        }

        public static decimal DeserializeAsDecimal(string value)
        {
            return XmlConvert.ToDecimal(value);
        }

        public static DateTime DeserializeAsDateTime(string value)
        {
            return XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Utc).ToLocalTime();
        }

        public static bool DeserializeAsBoolean(string value)
        {
            return XmlConvert.ToBoolean(value);
        }

        public static Guid DeserializeAsUniqueIdentifier(string value)
        {
            return XmlConvert.ToGuid(value);
        }

        public static XmlDocument DeserializeAsXml(string value)
        {
            XmlDocument result = null;
            if (!String.IsNullOrEmpty(value))
            {
                result = new XmlDocument();
                result.LoadXml(value);
            }
            return result;
        }
    }
}
